package Ventanas.usuario;

import java.awt.Dimension;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

import util.SocketManager;
import util.Varias;

public class Ventana extends JFrame{

	private JPanel panel;
	public static int puerto = 3000;
	public static SocketManager manager;

	public Ventana(){
		this.setVisible(true);
		setSize(800,600);
		this.setPreferredSize(new Dimension (800,600));
		setResizable(false);	
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);

		panel = new PanelInicial(this);

		pack();
	}



	public boolean conectar(String ip)
	{
		boolean conexion = false;
		JOptionPane.showMessageDialog(null, "Conectando a "+ip, ip, JOptionPane.INFORMATION_MESSAGE);
		/*if(Varias.verificarIP(ip))
		{*/
			try {
				manager = new SocketManager(ip,puerto);
				System.out.println("Conectado a la direccion" + ip);
				conexion = true;
				JOptionPane.showMessageDialog(null, "Conexion correcta", ip, JOptionPane.INFORMATION_MESSAGE);
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "Error de conexion, intentelo mas tarde", ip, JOptionPane.ERROR_MESSAGE);
			}
		/*}
		else
		{
			JOptionPane.showMessageDialog(null, "Dir. IP Erronea", "IP Erronea", JOptionPane.ERROR_MESSAGE);
		}*/
		return conexion;

	}

	public static void escribir (String comando)
	{

		try {
			manager.Escribir(comando);
		} catch (IOException e) {
			System.out.println("Error al escribir el comando "+comando);
		}

	}

	public static String leer()
	{

		String respuesta = "";
		try {
			respuesta=manager.Leer();
		} catch (IOException e) {
			System.out.println("Error al leer la respuesta.");
		}
		return respuesta;
	}

	public static void salir()
	{

		try {
			manager.CerrarStreams();
			manager.CerrarSocket();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
